//
//  EMMHousingCell.m
//  EMMApp
//
//  Created by Mac on 16/6/30.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "EMMHousingCell.h"
/** Cell
 *
 *  Cell导入.h文件
 */
#import "HousingCVCell.h"

@interface EMMHousingCell () {
    NSInteger num;
}

/**
 *  数据源数组
 */
@property (nonatomic, strong) NSMutableArray *subDataArr;

@end

@implementation EMMHousingCell

#pragma mark - 初始化
- (void)awakeFromNib {
    [super awakeFromNib];
    _viewONe.backgroundColor = [UIColor whiteColor];
    _ysNameLabel.textColor = [UIColor colorFromHexCode:@"#f0565e"];
    _ysNumLabel.layer.masksToBounds = YES;
    _ysNumLabel.layer.cornerRadius = 8;
    _ysNumLabel.backgroundColor = [UIColor colorFromHexCode:@"#f0565e"];

    _ksNameLabel.textColor = [UIColor colorFromHexCode:@"#6ac96b"];
    _kyNumLabel.layer.masksToBounds = YES;
    _kyNumLabel.layer.cornerRadius = 8;
    _kyNumLabel.backgroundColor = [UIColor colorFromHexCode:@"#6ac96b"];
    _subDataArr = [[NSMutableArray alloc] init];
}

#pragma mark - 初始化 传入数据源和第几行
- (void) initWithNSMutableArray:(NSMutableArray *)mArr indexNum:(NSInteger)indexNum {
    _subDataArr = mArr;
    num = indexNum;
    [self creatCollectionView];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

#pragma mark - UICollectionView初始化
- (void)creatCollectionView {    
    UICollectionViewFlowLayout *layuet = [[UICollectionViewFlowLayout alloc] init];
    //垂直行间距
    layuet.minimumLineSpacing = 1;
    //水平间隔
    layuet.minimumInteritemSpacing = 1;
    
    [layuet setScrollDirection:UICollectionViewScrollDirectionVertical];
    float h = [_subDataArr[num] count]/4.00;
    self.homeCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(15, 51, EMM_WIDTH-30, 60*ceilf(h)) collectionViewLayout:layuet];
    [self.homeCollectionView registerNib:[UINib nibWithNibName:@"HousingCVCell" bundle:nil] forCellWithReuseIdentifier:@"HousingCVCell"];
    self.homeCollectionView.delegate = self;
    self.homeCollectionView.dataSource = self;
    self.homeCollectionView.backgroundColor = [UIColor colorFromHexCode:@"f6fafb"];
    self.homeCollectionView.scrollEnabled = NO;
    [self addSubview:self.homeCollectionView];
}

#pragma mark - UICollectionViewDataSource / UICollectionViewDelegate /UICollectionViewDelegateFlowLayout 代理
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [_subDataArr[num] count] ;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"HousingCVCell";
    HousingCVCell *homeCell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    homeCell.backgroundColor = [UIColor colorFromHexCode:@"f6fafb"];
    BuildingList *bList = [_subDataArr[num] objectAtIndex:indexPath.row];//[_subDataArr objectAtIndex:indexPath.row];
    homeCell.titleLabel.layer.masksToBounds = YES;
    homeCell.titleLabel.layer.cornerRadius = 4;
    homeCell.titleLabel.backgroundColor = [UIColor colorFromHexCode:@"#f0565e"];
    homeCell.titleLabel.text = bList.buildingName;
    if ([NSString isStringEmpty2:bList.isSell] == NO) {
        if ([bList.isSell isEqualToString:@"0"]) {
            homeCell.titleLabel.backgroundColor = [UIColor colorFromHexCode:@"#f0565e"];
        }else {
            homeCell.titleLabel.backgroundColor = [UIColor colorFromHexCode:@"#6ac96b"];
        }
    }
    return homeCell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((EMM_WIDTH-20)/4, 60);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.delegate!=nil &&[self.delegate respondsToSelector:@selector(didSelectedTableview:withcollecttion:)]) {
        [self.delegate didSelectedTableview:num withcollecttion:indexPath];
    }
}

@end
